
import 'package:flutter/material.dart';
import 'package:moonplay/utils/utils.dart';
import 'package:cached_network_image/cached_network_image.dart';

import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:moonplay/common/app_common.dart';

/// 加载本地资源图片
Widget loadAssetImage(String name, {double width, double height, BoxFit fit, String format = "png"}){
  return Image.asset(
    Utils.getImgPath(name, format: format),
    height: height,
    width: width,
    fit: fit
  );
}

/// 加载网络图片
Widget loadNetworkImage(String imageUrl, {String placeholder : "img/placeholder", double width, double height, BoxFit fit: BoxFit.fill}){
  return CachedNetworkImage(
    imageUrl: imageUrl == "" || imageUrl == null ? "http://app.ydecai.cn/upload/vod/placeholder.png" : imageUrl,
    placeholder: (context, url) => loadAssetImage(placeholder, height: height, width: width, fit: fit),
    errorWidget: (context, url, error) => loadAssetImage(placeholder, height: height, width: width, fit: fit),
    width: width,
    height: height,
    fit: fit,
  );
}

class ImageUtils {
  Stream<FileResponse> fileStream;

  var cacheData = {
    "url": "http://afbf245cf4685704efdb.qiniucdn.apicloud-system.com/apicloud/a0f9a9c24afb06c1b62d6876924253f7.jpg",
    "type": "img"
  };

  ///缓存文件
  cacheFile(){
    fileStream = DefaultCacheManager().getFileStream(cacheData['url']);
    // var _date = fileStream.first as FileInfo;
    logger.d(fileStream);
  }

  ///清空缓存
  void _clearCache() {
    DefaultCacheManager().emptyCache();
    fileStream = null;
  }
}
